﻿using System;


using System.Text;
using IAM.Interface.Model.Interface;
using System.Xml;
using System.Reflection;

namespace IAM.Interface.Model
{
    /// <summary>
    /// 权限模型中所有权限维度的列表
    /// 包含1个或多个权限维度Dimension
    /// </summary>
    public class Dimensions : AbstractConvertObject,IConvertXml
    {
        public Dimensions()
        {
            _dimensions = new System.Collections.ArrayList();
        }

        #region 属性
        private System.Collections.ArrayList _dimensions;
        /// <summary>
        /// 权限模型中所有权限维度的列表 Dimension集合
        /// </summary>
        public System.Collections.ArrayList Items
        {
            set
            {
                _dimensions = value;
            }
            get
            {
                return _dimensions;
            }
        }
        /// <summary>
        /// 权限维度的列表数量
        /// </summary>
        public int Count
        {
            get
            {
                return Items.Count;
            }
        }
        #endregion
        #region IConvertXml

        public string XMLBoday
        {
            get
            {
                StringBuilder _xml = new StringBuilder();
                _xml.Append("<Dimensions");
                _xml.Append(" Count=\"" + this.Count + "\"");
                _xml.Append(" >");
                foreach (System.Object dimension in Items)
                {
                    _xml.Append(((Dimension)dimension).XML);
                }

                return _xml.ToString();
            }


        }

        public string XMLFinis
        {
            get
            {
                return "</Dimensions>";
            }
        }
        public string XML
        {
            get
            {
                return XMLBoday + XMLFinis;
            }
        }
        #endregion
        #region AbstractConvertObject
        public override object ConvertToObject()
        {
            foreach (XmlNode xn in this.CurrentXmlNode.ChildNodes)
            {
                Dimension dimension = new Dimension();
                dimension.CurrentXmlNode = xn;
                dimension = (Dimension)dimension.ConvertToObject();
                this.Items.Add(dimension);
            }
            return this;
        }
        #endregion
        #region 重写Equals
        /// <summary>
        /// 确定指定的 DimensionLevels 对象是否等效于当前 DimensionLevels
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            Dimensions dls = null;
            try
            {
                dls = (Dimensions)obj;
            }
            catch
            {
                return false;
            }
            bool result = true;
            if (this.Items.Count != dls.Items.Count)
            {
                result = false;
            }
            else
            {
                foreach (object oneofsource in this.Items)
                {
                    Dimension dlsource = (Dimension)oneofsource;
                    result = false;

                    foreach (object oneofobj in dls.Items)
                    {
                        Dimension dlobj = (Dimension)oneofobj;
                        if (dlsource.Equals(dlobj))
                        {
                            result = true;
                            break;
                        }
                    }
                    if (result == false)
                    {
                        break;
                    }
                }
            }

            return result;
        }

        #endregion
        #region 重写GetHashCode
        /// <summary>
        /// 哈希函数
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            int result = 1;
            PropertyInfo[] pi = this.GetType().GetProperties();
            foreach (PropertyInfo onofpi in pi)
            {
                result = result + onofpi.GetHashCode();
            }
            return result;
        }
        #endregion
    }
}
